﻿using AdinZ.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/3 18:14:08
*
* */
namespace Novel.Sys.Dal
{
    public class SiteConfig
    {
        /// <summary>
        /// 得到一个对象实体 --- 系统配置
        /// </summary>
        public Model.SiteConfig GetModel_SysConfig()
        {
            string strXmlFile =System.Web.HttpContext.Current.Server.MapPath("~/config/sys.config");

            Model.SiteConfig model = new Model.SiteConfig();
            model.WebName = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//WebName").InnerText.Trim();
            model.Keyword = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//Keyword").InnerText.Trim();
            model.Description = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//Description").InnerText.Trim();
            model.InstallDir = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//InstallDir").InnerText.Trim();
            model.UrlMode = int.Parse(XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//UrlMode").InnerText.Trim());
            model.TemplateCache = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//TemplateCache").InnerText.Trim();
            model.WebPageCache = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//WebPageCache").InnerText.Trim();
            model.TemplateDir = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//TemplateDir").InnerText.Trim();
            model.CacheKey = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//CacheKey").InnerText.Trim();
            model.CacheTime = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//CacheTime").InnerText.Trim();
            model.FileHost = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//FileHost").InnerText.Trim();
            model.SiteHost = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//SiteHost").InnerText.Trim();

            model.RewriteChannelPrefix = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//RewriteChannelPrefix").InnerText.Trim();
            model.RewriteSpecialPrefix = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//RewriteSpecialPrefix").InnerText.Trim();
            model.RewriteContentPrefix = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//RewriteContentPrefix").InnerText.Trim();
            model.RewriteGuestbookPrefix = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//RewriteGuestbookPrefix").InnerText.Trim();

            model.RemoteImgDown = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//RemoteImgDown").InnerText.Trim();
            model.UpFileType = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//UpFileType").InnerText.Trim();
            model.UpFileMaxSize = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//UpFileMaxSize").InnerText.Trim();

            model.PageListNum = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//PageListNum").InnerText.Trim();
            model.ImageSeconds = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//ImageSeconds").InnerText.Trim();

            model.UserType = int.Parse(XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//UserType").InnerText.Trim());
            model.UserRank = int.Parse(XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//UserRank").InnerText.Trim());
            model.UserScores = int.Parse(XMLHelper.GetXmlNodeByXpath(strXmlFile, "//sys_configuration//UserScores").InnerText.Trim());

            return model;
        }


        /// <summary>
        /// 更新 --- 系统配置
        /// </summary>
        public bool Update_SysConfig(Model.SiteConfig model)
        {
            string strXmlFile = HttpContext.Current.Server.MapPath("~/config/sys.config");

            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "WebName", "<![CDATA[" + model.WebName + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "Keyword", "<![CDATA[" + model.Keyword + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "Description", "<![CDATA[" + model.Description + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "InstallDir", "<![CDATA[" + model.InstallDir + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "UrlMode", model.UrlMode.ToString());
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "TemplateCache", model.TemplateCache);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "WebPageCache", model.WebPageCache);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "TemplateDir", "<![CDATA[" + model.TemplateDir + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "CacheTime", model.CacheTime);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "FileHost", model.FileHost);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "SiteHost", model.SiteHost);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "RewriteChannelPrefix", "<![CDATA[" + model.RewriteChannelPrefix + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "RewriteSpecialPrefix", "<![CDATA[" + model.RewriteSpecialPrefix + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "RewriteContentPrefix", "<![CDATA[" + model.RewriteContentPrefix + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "RewriteGuestbookPrefix", "<![CDATA[" + model.RewriteGuestbookPrefix + "]]>");

            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "RemoteImgDown", model.RemoteImgDown);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "UpFileType", "<![CDATA[" + model.UpFileType + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "UpFileMaxSize", model.UpFileMaxSize);

            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "PageListNum", model.PageListNum);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "ImageSeconds", model.ImageSeconds);

            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "UserType", model.UserType.ToString());
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "UserRank", model.UserRank.ToString());
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//sys_configuration", "UserScores", model.UserScores.ToString());

            return true;
        }

        /// <summary>
        /// 得到一个对象实体 --- SEO
        /// </summary>
        public Model.SiteConfig GetModel_SeoConfig()
        {
            string strXmlFile = HttpContext.Current.Server.MapPath("~/config/seo.config");

            Model.SiteConfig model = new Model.SiteConfig();
            model.AutoDescription = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//seo_configuration//AutoDescription").InnerText.Trim();
            model.SeoTitle = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//seo_configuration//SeoTitle").InnerText.Trim();
            model.IndexKeywords = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//seo_configuration//IndexKeywords").InnerText.Trim();
            model.IndexDescription = XMLHelper.GetXmlNodeByXpath(strXmlFile, "//seo_configuration//IndexDescription").InnerText.Trim();

            return model;
        }
        /// <summary>
        /// 更新 --- SEO
        /// </summary>
        public bool Update_SeoConfig(Model.SiteConfig model)
        {
            string strXmlFile = HttpContext.Current.Server.MapPath("~/config/seo.config");

            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//seo_configuration", "AutoDescription", model.AutoDescription);
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//seo_configuration", "SeoTitle", "<![CDATA[" + model.SeoTitle + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//seo_configuration", "IndexKeywords", "<![CDATA[" + model.IndexKeywords + "]]>");
            XMLHelper.CreateOrUpdateXmlNodeByXPath(strXmlFile, "//seo_configuration", "IndexDescription", "<![CDATA[" + model.IndexDescription + "]]>");

            return true;
        }


    }
}
